package com.huanxing.cloud.mall.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanxing.cloud.mall.api.mapper.UserWithdrawalApplyMapper;
import com.huanxing.cloud.mall.api.service.UserWithdrawalApplyService;
import com.huanxing.cloud.mall.api.utils.HxTokenHolder;
import com.huanxing.cloud.mall.common.constant.MallCommonConstants;
import com.huanxing.cloud.mall.common.dto.UserWithdrawalApplyRequest;
import com.huanxing.cloud.mall.common.entity.UserWithdrawalApply;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class UserWithdrawalApplyServiceImpl extends ServiceImpl<UserWithdrawalApplyMapper, UserWithdrawalApply>
		implements UserWithdrawalApplyService {

	@Override
	public boolean add(UserWithdrawalApplyRequest userWithdrawalApplyRequest) {
		UserWithdrawalApply userWithdrawalApply = new UserWithdrawalApply();
		BeanUtil.copyProperties(userWithdrawalApplyRequest, userWithdrawalApply);
		userWithdrawalApply.setUserId(HxTokenHolder.getMallUserId());
		userWithdrawalApply.setStatus(MallCommonConstants.WITHDRAWAL_APPLY_STATUS_1);
		// 计算手续费
		BigDecimal rate = new BigDecimal(MallCommonConstants.WITHDRAWAL_APPLY_RATE).divide(new BigDecimal("100"), 2,
				RoundingMode.HALF_EVEN);
		userWithdrawalApply.setServiceFee(userWithdrawalApplyRequest.getApplyAmount().multiply(rate));
		userWithdrawalApply
			.setPaymentAmount(userWithdrawalApply.getApplyAmount().subtract(userWithdrawalApply.getServiceFee()));
		return this.save(userWithdrawalApply);
	}

}
